﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CNDM.UserControls.List
{
    public partial class ucList : System.Web.UI.UserControl
    {
        private string cat_displayurl = "";
        private int pageindex = 1;
        private int pagesize = 10;
        public string Cat_ID = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["cat_displayurl"] != null && Request.QueryString["cat_displayurl"].ToString() != "")
            {
                cat_displayurl = Request.QueryString["cat_displayurl"].ToString();
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt = BO.Category.CategoryData.Get_Category_by_CatUrl(cat_displayurl);
            if (dt != null && dt.Rows.Count > 0)
            {
                Cat_ID = dt.Rows[0]["Cat_id"].ToString();
                ltrCatName.Text = dt.Rows[0]["Cat_Name"].ToString();
                DataTable dtList = new DataTable();
                dtList = BO.News.NewsData.Get_DanhSachTin("0", Cat_ID, pageindex, pagesize);
                repList.DataSource = dtList;
                repList.DataBind();

                SetPageTitle(dt.Rows[0]["Cat_Name"].ToString(), dt.Rows[0]["Cat_Name"].ToString());
            }
        }

        private void SetPageTitle(string desc, string title)
        {
            var page = (Page)HttpContext.Current.Handler;
            page.Title = title + " - Cẩm nang điện máy";
            page.MetaDescription = desc + " - Cẩm nang điện máy";
            page.MetaKeywords = desc + ",cẩm nang điện máy";
        }

        protected void repList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image imgNews = (Image)e.Item.FindControl("imgNews");
                if (DataBinder.Eval(e.Item.DataItem, "News_Image").ToString().Trim() == "")
                {
                    imgNews.Visible = false;
                }
                else
                {
                    imgNews.Visible = true;
                    imgNews.ImageUrl = DataBinder.Eval(e.Item.DataItem, "Image_Url").ToString();
                }


            }
        }
    }
}